package alert
import (
"github.com/K-Phoen/sdk"
)
type ErrorMode string
const ErrorAlerting ErrorMode = "Alerting"
const ErrorKO ErrorMode = "Error"
const ErrorOK ErrorMode = "OK"
type NoDataMode string
const NoDataEmpty NoDataMode = "NoData"
const NoDataAlerting NoDataMode = "Alerting"
const NoDataOK NoDataMode = "OK"
type Option func (alert *Alert )
type Channel struct {
ID uint `json:"id"`
UID string `json:"uid"`
Name string `json:"Name"`
Type string `json:"type"`
}
const alertConditionRef = "_alert_condition_"
type Alert struct {
Builder *sdk .Alert
Datasource string
DashboardUID string
PanelID string
}
func New (name string , options ...Option ) *Alert {
nope := false
alert := &Alert {
Builder : &sdk .Alert {
Name : name ,
Rules : []sdk .AlertRule {
{
GrafanaAlert : &sdk .GrafanaAlert {
Title : name ,
Condition : alertConditionRef ,
Data : []sdk .AlertQuery {
{
RefID : alertConditionRef ,
QueryType : "" ,
DatasourceUID : "-100" ,
Model : sdk .AlertModel {
RefID : alertConditionRef ,
Type : "classic_conditions" ,
Hide : &nope ,
Datasource : sdk .AlertDatasourceRef {
UID : "-100" ,
Type : "__expr__" ,
},
Conditions : []sdk .AlertCondition {},
},
},
},
},
Annotations : map [string ]string {},
Labels : map [string ]string {},
},
},
},
}
for _ , opt := range append (defaults (), options ...) {
opt (alert )
}
return alert
}
func defaults() []Option {
return []Option {
EvaluateEvery ("1m" ),
For ("5m" ),
OnNoData (NoDataEmpty ),
OnExecutionError (ErrorAlerting ),
}
}
func (alert *Alert ) HookDatasourceUID (uid string ) {
for _ , rule := range alert .Builder .Rules {
for i := range rule .GrafanaAlert .Data {
query := &rule .GrafanaAlert .Data [i ]
if query .RefID == alertConditionRef {
continue
}
query .DatasourceUID = uid
query .Model .Datasource .UID = uid
}
}
}
func (alert *Alert ) HookDashboardUID (uid string ) {
for _ , rule := range alert .Builder .Rules {
rule .Annotations ["__dashboardUid__" ] = uid
}
}
func (alert *Alert ) HookPanelID (id string ) {
for _ , rule := range alert .Builder .Rules {
rule .Annotations ["__panelId__" ] = id
}
}
func Summary (content string ) Option {
return func (alert *Alert ) {
alert .Builder .Rules [0 ].Annotations ["summary" ] = content
}
}
func Description (content string ) Option {
return func (alert *Alert ) {
alert .Builder .Rules [0 ].Annotations ["description" ] = content
}
}
func Runbook (url string ) Option {
return func (alert *Alert ) {
alert .Builder .Rules [0 ].Annotations ["runbook_url" ] = url
}
}
func For (duration string ) Option {
return func (alert *Alert ) {
alert .Builder .Rules [0 ].For = duration
}
}
func EvaluateEvery (interval string ) Option {
return func (alert *Alert ) {
alert .Builder .Interval = interval
}
}
func OnExecutionError (mode ErrorMode ) Option {
return func (alert *Alert ) {
alert .Builder .Rules [0 ].GrafanaAlert .ExecutionErrorState = string (mode )
}
}
func OnNoData (mode NoDataMode ) Option {
return func (alert *Alert ) {
alert .Builder .Rules [0 ].GrafanaAlert .NoDataState = string (mode )
}
}
func If (reducer QueryReducer , queryRef string , evaluator ConditionEvaluator ) Option {
return ifOperand (And , reducer , queryRef , evaluator )
}
func IfOr (reducer QueryReducer , queryRef string , evaluator ConditionEvaluator ) Option {
return ifOperand (Or , reducer , queryRef , evaluator )
}
func ifOperand(operand Operator , reducer QueryReducer , queryRef string , evaluator ConditionEvaluator ) Option {
return func (alert *Alert ) {
cond := newCondition (reducer , queryRef , evaluator )
cond .builder .Operator = sdk .AlertOperator {Type : string (operand )}
alert .Builder .Rules [0 ].GrafanaAlert .Data [0 ].Model .Conditions = append (alert .Builder .Rules [0 ].GrafanaAlert .Data [0 ].Model .Conditions , *cond .builder )
}
}
func Tags (tags map [string ]string ) Option {
return func (alert *Alert ) {
alert .Builder .Rules [0 ].Labels = tags
}
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .